/**
 *  大气环境预警预报应用-污染传输影响分析
 *  Created by dinghh on 2018/7/18.
 */
'use strict';
angular.module("MetronicApp").directive('monitorMap', function () {
    return {
        scope: {
            id: "@",
            data: "="
        },
        restrict: 'EA',
        template: '<div style="width: 600px;height: 800px"></div>',
        replace: true,
        link: function ($scope, element, attrs, controller) {
            var myChart = echarts.init(document.getElementById($scope.id), 'macarons');
            var uploadedDataURL = "../../../data/w/decisionAnalysis/prediction/pollutantSource/hebei.json";
            myChart.showLoading();

            $.getJSON(uploadedDataURL, function(geoJson) {
                echarts.registerMap('hebei', geoJson);
                myChart.hideLoading();
                var geoCoordMap = {
                    "承德市": [117.5757,41.4075],
                    "承德市公园1": [117.2356,41.1100],
                    "承德市公园2": [117.1056,41.7819],
                    "张家口市": [115.1477,40.8527],
                    "张家口市公园": [115.1477,40.2427],
                    "衡水市": [115.8838,37.7161],
                    "衡水市公园": [115.6838,37.4161],
                    "邯郸市": [114.4775,36.535],
                    "沧州市": [116.8286,38.2104],
                    "邢台市": [114.8071,37.2821],
                    "秦皇岛市": [119.2126,40.0232],
                    "石家庄市": [114.4995,38.1006],
                    "保定市": [115.0488,39.0948],
                    "廊坊市": [116.521,39.0509],
                    "唐山市": [118.4766,39.6826]
                }
                var data = [
                    {'name':'承德市', value:100,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'11'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                    ]},
                    {'name':'承德市公园1', value:1,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'111'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'承德市公园2', value:1,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'112'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'张家口市',value:100,
                        'items':[
                        {'itemName':'SO2','value':'0.035','index':'12'},
                        {'itemName':'CO','value':'1.271','index':'14'},
                        {'itemName':'NO2','value':'0.032','index':'16'},
                        {'itemName':'03-1H','value':'0.119','index':'38'},
                        {'itemName':'03-8H','value':'0.083','index':'0'},
                        {'itemName':'PM2.5','value':'0.029','index':'42'},
                        {'itemName':'PM10','value':'0.11','index':'80'}
                    ]},
                    {'name':'张家口市公园', value:1,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'121'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'衡水市',value:50,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'13'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'衡水市公园', value:1,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'131'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'邯郸市',value:50,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'14'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'沧州市',value:50,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'15'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'邢台市',value:50,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'16'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'秦皇岛市',value:100,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'17'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'石家庄市',value:50,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'18'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'保定市',value:50,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'19'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'廊坊市',value:50,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'20'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]},
                    {'name':'唐山市',value:50,
                        'items':[
                            {'itemName':'SO2','value':'0.035','index':'21'},
                            {'itemName':'CO','value':'1.271','index':'14'},
                            {'itemName':'NO2','value':'0.032','index':'16'},
                            {'itemName':'03-1H','value':'0.119','index':'38'},
                            {'itemName':'03-8H','value':'0.083','index':'0'},
                            {'itemName':'PM2.5','value':'0.029','index':'42'},
                            {'itemName':'PM10','value':'0.11','index':'80'}
                        ]}
                ];

                var convertData = function(data) {
                    var res = [];
                    for (var i = 0; i < data.length; i++) {
                        var geoCoord = geoCoordMap[data[i].name];
                        if (geoCoord) {
                            res.push({
                                name: data[i].name,
                                item: data[i].items,
                                value: geoCoord.concat(data[i].value)
                            });
                        }
                    }
                    return res;
                };

                var option = {
                    tooltip: {
                        trigger: 'item',
                        triggerOn:'click',
                        formatter: function(params) {
                            var item = params.data.item;
                            var texts = "";
                            texts += '市区世纪公园监测点<br\>'+'<div class="row">'
                                +'<div class="col-sm-4" style="text-align: center">监测项目'
                                +'<ul style="list-style-type:none">'
                                +'<li>'+item[0].itemName+'</li>'
                                +'<li>'+item[1].itemName+'</li>'
                                +'<li>'+item[2].itemName+'</li>'
                                +'<li>'+item[3].itemName+'</li>'
                                +'<li>'+item[4].itemName+'</li>'
                                +'<li>'+item[5].itemName+'</li>'
                                +'<li>'+item[6].itemName+'</li>'
                                +'</ul>'
                                +'</div>'
                                +'<div class="col-sm-4" style="text-align: center">监测值(mg/m3)'
                                +'<ul style="list-style-type:none">'
                                +'<li>'+item[0].value+'</li>'
                                +'<li>'+item[1].value+'</li>'
                                +'<li>'+item[2].value+'</li>'
                                +'<li>'+item[3].value+'</li>'
                                +'<li>'+item[4].value+'</li>'
                                +'<li>'+item[5].value+'</li>'
                                +'<li>'+item[6].value+'</li>'
                                +'</ul>'
                                +'</div>'
                                +'<div class="col-sm-4" style="text-align: center">分指数'
                                +'<ul style="list-style-type:none">'
                                +'<li>'+item[0].index+'</li>'
                                +'<li>'+item[1].index+'</li>'
                                +'<li>'+item[2].index+'</li>'
                                +'<li>'+item[3].index+'</li>'
                                +'<li>'+item[4].index+'</li>'
                                +'<li>'+item[5].index+'</li>'
                                +'<li>'+item[6].index+'</li>'
                                +'</ul>'
                                +'</div>'
                                +'</div>';
                            return texts;
                        }
                    },
                    visualMap: {
                        show: false,
                        min: 50,
                        max: 100,
                        left: 'left',
                        top: 'bottom',
                        text: ['高', '低'], // 文本，默认为数值文本
                        calculable: true,
                        seriesIndex: [0],
                        inRange: {
                            color: ['#F98355','#01AAE9'] //
                        }
                    },
                    geo: {
                        show: true,
                        map: 'hebei',
                        data:convertData(data),
                        label: {
                            normal: {
                                show: false
                            },
                            emphasis: {
                                show: false
                            }
                        },
                        itemStyle: {
                            normal: {
                                // areaColor: '#2B91B7'
                            },
                            emphasis: {
                                areaColor: ''
                            }
                        }
                    },
                    series: [
                        {
                            type: 'map',
                            map: 'hebei',
                            geoIndex: 0,
                            data: data,
                            itemStyle: {
                                normal: {
                                    areaColor: '#ffffff'
                                }
                            }
                        },
                        {
                            name: '公园',
                            type: 'scatter',
                            coordinateSystem: 'geo',
                            data: convertData(data),
                            symbolSize: 7,
                            label: {
                                normal: {
                                    formatter: '{b}',
                                    position: 'right',
                                    show: false
                                }
                            },
                            itemStyle: {
                                normal: {
                                    color: '#ffffff'
                                }
                            }
                        },
                        {
                            name: '城市',
                            type: 'effectScatter',
                            coordinateSystem: 'geo',
                            data: convertData(data.sort(function (a, b) {
                                return b.value - a.value;
                            }).slice(0, 11)),
                            symbolSize: 7,
                            label: {
                                normal: {
                                    formatter: '{b}',
                                    position: 'top',
                                    show: true
                                }
                            },
                            itemStyle: {
                                normal: {
                                    color: '#ffffff'
                                }
                            }
                        }
                    ]
                };
                myChart.setOption(option);
                myChart.on('click', function(data){
                    convertData(data)
                });
            });
        }
    };
});
